package migrations

import (
	"gorm.io/gorm"

	"github.com/runningwater/gohub/pkg/console"
	"github.com/runningwater/gohub/pkg/migrate"
)

func init() {

	type User struct {
		City         string `gorm:"type:varchar(10);"`
		Introduction string `gorm:"type:varchar(255);"`
		Avatar       string `gorm:"type:varchar(255);default:null"`
	}

	up := func(migrator gorm.Migrator, DB *gorm.DB) {
		_ = DB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci").AutoMigrate(&User{})
	}

	down := func(migrator gorm.Migrator, DB *gorm.DB) {
		err := migrator.DropColumn(&User{}, "city")
		if err != nil {
			console.Error(err.Error())
			return
		}
		err = migrator.DropColumn(&User{}, "introduction")
		if err != nil {
			console.Error(err.Error())
			return
		}
		err = migrator.DropColumn(&User{}, "avatar")
		if err != nil {
			console.Error(err.Error())
			return
		}
	}

	migrate.Add(up, down, "2025_07_22_100425_add_fields_to_user")
}
